package com.zhugang.week05;

import com.zhugang.week04.IsAnagram;

/**
 * @program algorithms
 * @description: isAnagram
 * @author: chanzhugang
 * @create: 2022/07/08 17:07
 */
public class Anagram {

    public static void main(String[] args) {
        IsAnagram isAnagram = new IsAnagram();
        String s = "nl";
        String t = "cx";
        boolean flag = isAnagram.isAnagram(s, t);
        System.out.println(flag);
    }

    /**
     * 242 有效的字母异位词
     *
     * @param s
     * @param t
     * @return
     */
    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }
        int[] nums1 = new int[26];
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            nums1[c - 'a']++;
        }
        int[] nums2 = new int[26];
        for (int i = 0; i < t.length(); i++) {
            char c = t.charAt(i);
            nums2[c - 'a']++;
        }

        for (int i = 0; i < 26; i++) {
            if (nums1[i] != nums2[i]) {
                return false;
            }
        }
        return true;
    }
}